<template>
  <div class="login">
    <div class="ms_login">
      <h3 style="text-align:center">云音乐后台管理系统</h3>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item prop="name">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="ruleForm.name"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-s-goods"
            v-model="ruleForm.password"
            show-password
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="login()">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        password: ""
      },
      rules: {
        name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "change" }]
      }
    };
  },
  methods: {
    login() {
      this.$http
        .get("http://localhost:8090/login", {
          params: {
            username: this.ruleForm.name,
            password: this.ruleForm.password
          }
        })
        .then(result => {
          //成功返回的回调函数
          if (result.data.code == 101) {
            //成功
            this.$message({
              message: result.data.msg,
              type: "success"
            });
            // cookie  localStorage 前端存储数据
            // json数据转换为字符串类型
            //将数据存入到localStorage
            localStorage.setItem("vo", JSON.stringify(result.data.data));
            //跳转首页
            this.$router.push("/home");
          } else {
            //失败
            this.$message.error(result.data.msg);
          }
        })
        .catch(err => {
          //抛异常显示的内容
        });
    }
  }
};
</script>

<style scoped>
.login {
  width: 100%;
  height: 100%;
  background: #42b983;
}
.ms_login {
  width: 300px;
  height: 250px;
  position: absolute;
  left: 50%;
  top: 50%;
  padding: 40px;
  margin: -150px 0 0 -190px;
  background: #fff;
  border-radius: 5px;
}

.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
}
</style>
